package Game;

import java.awt.Graphics2D;
import java.awt.geom.Point2D;

import Game.PoliticAlly.PoliticAllyType;

public class CPI extends Ally
{
	boolean spawnDead;
	float spawnDelay;
	float c;
	
	float attackDelay;
	float c2;
	
	boolean spawnMove;
	
	public CPI(int x, int y, int w, int h, int spawnDelay, int id)
	{
		super(x, y, w, h, id);
		
		Load("/Images/cpi.png");
		
		lifeBar = new EnergyBar(new Point2D.Double(x, y + 130), new Point2D.Double(150, 10), 10, false);
		
		state = AllyState.IDLE;
		this.type = AllyType.CPI;
		
		this.spawnDelay = spawnDelay;
		c = spawnDelay;
		
		attackDelay = 3;
		c2 = 0;
		
		spawnDead = true;
		spawnMove = false;
	}
	
	public void Update()
	{
		if(state != AllyState.DEAD)
		{
			if(lifeBar.CurrentEnergy <= 0)
			{
				Kill();
			}
			
			add_remove = false;
			
			if(spawnDead)
			{
				c++;
				state = AllyState.IDLE;
				
				if(c >= spawnDelay)
				{
					c = 0;
					AllyManager.AddPolitic(new PoliticAlly((int)(x + (h * 0.25f)), y, (int)(70 * 1.0f), (int)(122 * 1.0f), PoliticAllyType.VEREADOR, id));
					add_remove = true;
					System.out.println("CPI Sumonou vereador");
					spawnDead = false;
					spawnMove = false;
				}
			}
			
			c2 += 0.1f;
			UpdateCollision();
			
			if(AllyManager.politics != null)
			{
				for(PoliticAlly politic : AllyManager.politics)
				{
					if(politic.id == this.id)
					{
						if(politic.getRectangle().intersects(getRectangle()))
						{
							politic.move = true;
							spawnMove = true;
						}
						else
						{
							if(spawnMove)
							{
								politic.move = true;
							}
							else
							{
								politic.move = false;
							}
						}
					}
				}
			}
		}		
	}

	private void UpdateCollision()
	{
		if(EnemyManager.inimigos != null)
		{
			for(Enemy enemy : EnemyManager.inimigos)
			{
				if(this.getRectangle().intersects(enemy.getRectangle()))
				{
					if(enemy.visible)
					{
						if(c2 >= attackDelay)
						{
							enemy.lifeBar.CurrentEnergy -= 1;
							c2 = 0;
						}
					}
				}
			}
		}
		
		if(Mouse.clicked)
		{
			if(getRectangle().contains(Mouse.lastClickedPosition))
			{
				for(PoliticAlly politic : AllyManager.politics)
				{
					if(politic.id == this.id)
					{
						if(!politic.getRectangle().contains(Mouse.lastClickedPosition))
						{
							spawnMove = !spawnMove;
							politic.move = spawnMove;
						} 
					}
				}
			}
		}
	}
	
	public void Draw(Graphics2D g2d)
	{
		super.Draw(g2d);
	}

	@Override
	public void Attack()
	{
		
	}

	@Override
	public void Kill()
	{
		state = AllyState.DEAD;
	}
}
